var app = getApp();
var req = require("../../utils/req.js");
var url = require("../../utils/url.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    consumption: [],
    current_page: 0,
    isLoadedAll: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getConsumption();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (!this.data.isLoadedAll) {
      this.data.current_page++;
      this.getConsumption();
    }
  },
  /**
   * 下拉刷新用户收藏的商品数据
   */
  onPullDownRefresh: function() {
    if (!this.data.isLoadedAll) {
      this.data.current_page++;
      this.getConsumption();
    }
    wx.stopPullDownRefresh();
  },
  /**
   * 消费记录
   */
  getConsumption: function() {
    var that = this;
    var storeId = app.globalData.storeId;
    console.log(storeId);
    var data = {
      page: that.data.current_page
    };



    req.storeRequest("请稍等", "POST", url.store['getConsumpitionList'], storeId, data, function back(res) {
      var data = res.data;
      console.log(res.data)
      if (that.data.consumption.length == 0 && data.length == 0) {
        wx.showToast({
          icon: 'none',
          title: '暂无记录',
        })
        return
      }
      if (data.length != 0) {
        that.data.consumption.push.apply(that.data.consumption, data);
        that.setData({
          consumption: that.data.consumption
        })
        console.log(that.data.current_page)
      } else {
        wx.showToast({
          icon: 'none',
          title: '已经到底了！',
        })
        that.data.isLoadedAll = true; //全部加载完毕
        that.data.current_page = 0; //当前页面重新设置成1
      }
    });
  }
})